package net.zcy.zalllonely.modules.study.controller;

import net.zcy.zalllonely.common.exception.ZallException;
import net.zcy.zalllonely.common.exception.enums.ResultEnum;
import net.zcy.zalllonely.common.exception.util.ResultUtil;
import net.zcy.zalllonely.modules.study.entity.Student;
import net.zcy.zalllonely.modules.study.protocol.StudentRequest;
import net.zcy.zalllonely.modules.study.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "/study")
public class StudentController {

    private static Logger LOG = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;


    @GetMapping("/getStudent")
    public Object getUser(@Valid StudentRequest request, BindingResult bindingResult, final Student student){

        LOG.info("根据查询条件获取用户信息");

        if(bindingResult.hasErrors()){
            throw new ZallException(ResultEnum.OTHER_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        Student resultStudent = studentService.selectOne(student);

        return ResultUtil.success(resultStudent);
    }
}
